<?php
require 'inc_security.php';
                 
$list = new dataGrid('com_id',20);
$list->add($name_field,'Tên','string',1,1, 'width="120px"');
$list->add('com_content','Nội dung','string');
$list->add('com_date','Ngày','int',0,1);
$list->add('','Link bình luận','string');
$list->add('','Sửa','edit');
$list->add('','Xóa','delete');
$db_count = new db_count('SELECT count(*) as count 
                            FROM '.$bg_table.'
                            WHERE 1 '.$list->sqlSearch().' '.$list->authorSearch($bg_table,$id_field).'
                            ');
$total = $db_count->total;unset($db_count);

$db_listing = new db_query('SELECT * 
                            FROM '.$bg_table.'
                            WHERE 1 '.$list->sqlSearch().' '.$list->authorSearch($bg_table,$id_field).'
                            ORDER BY '.$list->sqlSort().' com_date_feedback DESC, '.$id_field.' DESC
                            '.$list->limit($total));
$total_row = mysql_num_rows($db_listing->result);
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="vi" lang="vi" xmlns:og="http://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?=$load_header?>
</head>
<body>
<div class="wrapper">
   <div class="row">
      <div class="col-sm-12">
         <section class="panel">
            <header class="panel-heading">
               Danh sách <?=$module_name?>
            </header>
            <div class="panel-body">
               <?=$list->showHeader($total_row,'',1)?>
               <?php
               $i = 0; 
               ?>
               <?php while($row = mysql_fetch_assoc($db_listing->result)){
                 $i++;
                 $list_rep = db_array('SELECT * FROM comments_reply WHERE rep_comment_id = '.$row[$id_field].' ORDER BY rep_id ASC');
                 $count_rep = count($list_rep);
               ?>
               <?=$list->start_tr($i,$row[$id_field])?>
               <td>
                  <a class="llink" href="edit.php?record_id=<?=$row[$id_field]?>"><?=$row[$name_field]?></a>
                  <br /><p><?=$row['com_email']?></p>
               </td>
               <td>
                  <div class="lrow"><?=$row['com_content']?></div>
                  <p style="text-align: center; margin: 0;"><a onclick="repshow(<?=$i?>)" href="javascript:">Trả lời <?=$count_rep?></a></p>
               </td>
               <td>
                  <div class="lrow"><?=date('d/m/Y - H:ia',$row['com_date'])?></div>
               </td>
               <td>
               <?php
               switch($row['com_type']) {
                  case 'products' : 
                     $item = db_first('SELECT pro_id,pro_type,pro_name,pro_alias FROM products WHERE pro_id = '.$row['com_post_id']);
                     $item_name = $item['pro_name'];
                     $item_link = url_product_detail($item);
                     break;
                  default :
                     $item = db_first('SELECT pro_id,pro_type,pro_name,pro_alias FROM products WHERE pro_id = '.$row['com_post_id']);
                     $item_name = $item['pro_name'];
                     $item_link = url_product_detail($item);
                     break;
               }
               ?>
                  <a href="<?=$item_link?>" target="_blank"><?=$item_name?></a>
               </td>
               <?=$list->showEdit($row[$id_field])?>
               <?=$list->showDelete($row[$id_field])?>
               <?=$list->end_tr()?>
               <?php
               if($count_rep > 0) {
                  ?>
                  <tr id="rep-<?=$i?>" style="display: none;">
                     <td colspan="4">
                     <?
                     foreach($list_rep as $rep) {
                        ?>
                        <p>
                           <a id="repdel-<?=$rep['rep_id']?>" onclick="repdel(<?=$rep['rep_id']?>)" href="javascript:">(Xóa)</a>
                           <?=$rep['rep_admin'] == 0 ? '' : '<span style="background: #eee;">QTV</span>'?>
                           <b><?=$rep['rep_name']?> </b>
                           <span style="font-size: 12px;">(<?=date('H:ia d/m ',$rep['rep_date'])?>)</span> : 
                           <?=$rep['rep_content']?>
                        </p>
                        <?
                     }
                     ?>
                     </td>
                     <td colspan="4">
                        <textarea style="width: 100%;"></textarea><br />
                        <button class="admin_rep" data-id="<?=$row[$id_field]?>">Trả lời</button>
                     </td>
                  </tr>
                  <?
               }else {
                  ?>
                  <tr id="rep-<?=$i?>" style="display: none;">
                     <td colspan="4">
                     </td>
                     <td colspan="4">
                        <textarea style="width: 100%;"></textarea><br />
                        <button class="admin_rep" data-id="<?=$row[$id_field]?>">Trả lời</button>
                     </td>
                  </tr>
                  <?
               }
               ?>
               <?}?>
               <?=$list->showFooter()?>
            </div>
         </section>
      </div>
   </div>
</div>
<script>
function repshow(id) {
   $('#rep-'+id).show();
}
function repdel(id) {
   if(confirm('Bạn có chắc chắn muốn xóa trả lời này ?')) {
      $.ajax({
         type:'post',
         url:'ajax.php',
         data:{action:'del_reply',rep_id:id},
         dataType:'html',
         beforeSend:function(){
         },
         success:function(result){
            if(result == 1) {
               $('#repdel-'+id).closest('p').remove();
            }else {
               alert('Có lỗi xảy ra !');
            }
         }
      });
   }
}
$('.admin_rep').click(function(){
   var curr = $(this);
   var prtd = curr.closest('td');
   var rep_comment_id = curr.attr('data-id');
   var rep_content = prtd.find('textarea').val();
   if(rep_content == '') {
      alert('Sao lại để trống nội dung ô trả lời ? Làm ăn bố láo !');
      prtd.find('textarea').focus();
      return false;
   }else {
      $.ajax({
         type:'post',
         url:'ajax.php',
         data:{action:'add_reply',rep_comment_id:rep_comment_id,rep_content:rep_content},
         dataType:'html',
         beforeSend:function(){
            prtd.html('');
         },
         success:function(result){
            if(result == 1) {
               prtd.html('Trả lời thành công !');
            }else {
               prtd.html('Có lỗi xảy ra !');
            }
         }
      });
   }
});
</script>
</body>
</html>